package scct.yangxianpeng.dao;

import scct.yangxianpeng.model.GoodsType;
import scct.yangxianpeng.utils.DBUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class TypeDao {
    //    获取商品列表
    public List<GoodsType> getAllTyps() {
        List<GoodsType> list = new ArrayList<>();
        Connection conn = DBUtils.getConnection();
        String sql = "select * from sm_goodsType";
        PreparedStatement pstmt = null;
        try {
            pstmt = conn.prepareStatement(sql);
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                int id = rs.getInt("id");
                String name = rs.getString("name");
                String remark = rs.getString("remark");
                list.add(new GoodsType(id, name, remark));
            }
            DBUtils.closeConnection(conn);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return list;
    }

    //    添加商品列表
    public boolean addType(String name, String remark) {
        int result = 0;
        Connection conn = DBUtils.getConnection();
        String sql = "insert into sm_goodstype values(null,?,?)";
        try {
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, name);
            pstmt.setString(2, remark);
            result = pstmt.executeUpdate();
            DBUtils.closeConnection(conn);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return result > 0;
    }

    //    修改商品列表
    public boolean updateType(int id, String name, String remark) {
        int result = 0;
        Connection conn = DBUtils.getConnection();
        String sql = "update sm_goodstype set name=?,remark=? where id=?";
        try {
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, name);
            pstmt.setString(2, remark);
            pstmt.setInt(3,id);
            result = pstmt.executeUpdate();
            DBUtils.closeConnection(conn);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return result > 0;
    }

    //查询原数据将原数据显示在修改框中
    public GoodsType goodsTypeBtId(int id) {
        GoodsType goodsType=null;
        Connection conn=DBUtils.getConnection();
        String sql="select * from sm_goodstype where id=?";
        try {
            PreparedStatement pstmt=conn.prepareStatement(sql);
            pstmt.setInt(1,id);
            ResultSet rs=pstmt.executeQuery();
            if (rs.next()){
                String name=rs.getString("name");
                String remark=rs.getString("remark");
                goodsType=new GoodsType(id,name,remark);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return goodsType;
    }
//    public boolean isDumptype(String name) {
//
//    }
}
